/**
 * FileContentManager is a Java based file manager desktop application, it can
 * show, edit and manipulate the content of the files archived inside a zip.
 * 
 * Copyright (C) 2007
 * 
 * Created by Camila Sanchez [http://mimix.wordpress.com/], Rafael Naufal
 * [http://rnaufal.livejournal.com] and Rodrigo [rdomartins@gmail.com]
 * 
 * FileContentManager is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by the
 * Free Software Foundation; either version 2.1 of the License, or (at your
 * option) any later version.
 * 
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with FileContentManager; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
/*
 * Created on 31/05/2007 19:05:07
 */
package br.com.jteam.jfcm.gui.adapter;

import java.util.Iterator;

import br.com.jteam.jfcm.gui.PresentationFile;
import br.com.jteam.jfcm.gui.PresentationFileHistory;
import br.com.jteam.jfcm.model.File;
import br.com.jteam.jfcm.model.FileHistory;
import br.com.jteam.jfcm.model.FileType;
import br.com.jteam.jfcm.model.services.FileListContentService;
import br.com.jteam.jfcm.model.services.exception.FileListContentServiceException;

/**
 * <p>
 * Implementa&ccedil;&atilde;o do adaptador do servi&ccedil;o respons&aacute;vel
 * por manipular arquivos {@link br.com.minaurod.fcm.model.FileType#ZIP}
 * 
 * @author Rafael Naufal <rafael.naufal@gmail.com>
 * @since 1.0
 * @version $Id$
 */
public class FileListPresentationContentAdapterImpl
				implements
				FileListPresentationContentAdapter
{
	/**
	 * <p>
	 * Refer&ecirc;ncia para o servi&ccedil;o de manipula&ccedil;&atilde;o de
	 * arquivos.
	 */
	private FileListContentService fileListContentService;

	/**
	 * <p>
	 * Construtor.
	 * 
	 * @param fileListContentService -
	 *            servi&ccedil;o de manipula&ccedil;&atilde;o de arquivos.
	 * @throws IllegalArgumentException -
	 *             Caso o servi&ccedil;o de manipula&ccedil;&atilde;o de
	 *             arquivos seja nulo.
	 */
	public FileListPresentationContentAdapterImpl(FileListContentService fileListContentService)
	{
		if (fileListContentService == null)
		{
			throw new IllegalArgumentException("FileListContentService canoot be null");
		}
		this.fileListContentService = fileListContentService;
	}

	/**
	 * <p>
	 * Lista os arquivos que s&atilde;o conte&uacute;do de um arquivo
	 * {@link br.com.minaurod.fcm.model.FileType#ZIP}.
	 * 
	 * @param path -
	 *            caminho do arquivo .ZIP a ser aberto
	 * @return - uma inst�ncia da classe <code>PresentationFile</code>,
	 *         contendo uma lista de <code>PresentationFile</code>.
	 * @throws FileListContentAdapterException -
	 *             Caso o arquivo seja nulo ou n&atilde;o seja um arquivo
	 *             {@link br.com.minaurod.fcm.model.FileType#ZIP}
	 */
	public PresentationFile<?> openZipContent(String path) throws FileListContentAdapterException
	{
		if (path == null)
		{
			throw new IllegalArgumentException("File path cannot be null");
		}
		java.io.File file = new java.io.File(path);
		try
		{
			File zipFile = fileListContentService.openZipContent(file);
			PresentationFile<?> presentationZipFile =
							adapt2Presentation(zipFile);
			return presentationZipFile;
		} catch (FileListContentServiceException ex)
		{
			throw new FileListContentAdapterException(ex);
		}
	}

	@SuppressWarnings("unchecked")
	private PresentationFile<?> adapt2Presentation(File file)
	{
		PresentationFile<?> presentationFile =
						new PresentationFile(	file.getName(),
												file.getPath(),
												FileType.DEFAULT);
		Iterator<File> iterator = file.iterator();
		while (iterator.hasNext())
		{
			File fileToBeAdded = iterator.next();
			PresentationFile<?> presFile = adapt2Presentation(fileToBeAdded);
			presentationFile.add(presFile);
		}
		return presentationFile;
	}

	/**
	 * <p>
	 * Retorna o hist&oacute;rico de arquivos.
	 * 
	 * @return hist&oacute;rico de arquivos.
	 * @see br.com.minaurod.fcm.gui.adapter.FileListPresentationContentAdapter#getHistory()
	 */
	@SuppressWarnings("unchecked")
	public PresentationFileHistory getHistory()
	{
		FileHistory fileHistory = fileListContentService.getHistory();
		PresentationFileHistory presentationFileHistory =
						new PresentationFileHistory();
		for (File file : fileHistory)
		{
			presentationFileHistory.add(new PresentationFile(	file.getName(),
																file.getPath(),
																FileType.getFileTypeByFileName(file.getName())));
		}
		return presentationFileHistory;
	}
}
